package com.css.bjdt_core.reportPrepare.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Dao;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.cell.entity.VarParam;
import com.css.bjdt_core.cell.service.CustomSqlService;
import com.css.bjdt_core.reportPrepare.entity.RptUnit;
import com.css.bjdt_core.reportPrepare.entity.RptUnitAnnualRel;
import com.css.bjdt_core.util.Constants;

@Service
public class UnitTreeService extends CustomSqlService {

	@Autowired
	private Dao dao;
	@Autowired
	private RptUnitService rptUnitService;

	/**
	 * 构建党统党组织树对象(本下级)
	 * 
	 * @param dzzId
	 * @param annualId
	 * @return
	 */
	public List<RptUnit> buildUnitTreeObj(String dzzId, String annualId) {
		RptUnit rootUnit = rptUnitService.queryByDzzIdAndAnnualId(dzzId, annualId);
		List<RptUnit> units = rptUnitService.queryByPid(rootUnit.getId());
		if (units == null || units.size() == 0) {
			units = new ArrayList<RptUnit>();
		}
		units.add(rootUnit);
		return units;
	}

	public List<RptUnit> buildUnitTreeObj(String dzzId, String annualId, String name) {
		RptUnit rootUnit = rptUnitService.queryByDzzIdAndAnnualId(dzzId, annualId);
		List<RptUnit> units = rptUnitService.queryByPid(rootUnit.getId(), name);
		if (units == null || units.size() == 0) {
			units = new ArrayList<RptUnit>();
		}
		units.add(rootUnit);
		return units;
	}

	public List<RptUnit> buildUnit(String dzzId, String annualId) {
		RptUnit rootUnit = rptUnitService.queryByDzzIdAndAnnualId(dzzId, annualId);
		List<RptUnit> units = new ArrayList<RptUnit>();
		units.add(rootUnit);
		return units;
	}

	/**
	 * 构建党内党组织树对象(本下级)
	 * @return
	 */
	public List<RptUnit> buildDzzTreeObj(String dzzId) {
		return getUnitTreeByPid(dzzId, Constants.QUERY_ALL_CHILD, false);
	}

	/**
	 * 
	 * @param unitId
	 * @param sqlStr
	 * @param isNew
	 * @return
	 */
	private List<RptUnit> getUnitTreeByPid(String unitId, String sqlStr, final boolean isNew) {
		return executeSql(sqlStr, new SqlCallback() {

			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<RptUnit> list = new ArrayList<RptUnit>();
				while (rs.next()) {
					RptUnit unit = new RptUnit();
					RptUnitAnnualRel unitAnnualRel = new RptUnitAnnualRel();
					unitAnnualRel.setDzzId(rs.getString("code"));
					unit.setRel(unitAnnualRel);
					unit.setName(rs.getString("sname"));
					unit.setFullName(rs.getString("fname"));
					unit.setZzlb(rs.getString("zzlb"));
					if (!isNew) {
						unit.setId(rs.getString("code"));
						unit.setPid(rs.getString("pcode"));
					}
					list.add(unit);
				}
				return list;
			}

		}, new VarParam(null, null, "unitId", unitId)).getList(RptUnit.class);
	}

	public List<RptUnit> getUnitByPid(String dzzId, String annualId) {
		RptUnit rootUnit = rptUnitService.queryByDzzIdAndAnnualId(dzzId, annualId);
		List<RptUnit> units = rptUnitService.queryByPid(rootUnit.getId());
		if (units == null || units.size() == 0) {
			units = new ArrayList<RptUnit>();
		}
		units.add(rootUnit);
		return units;
	}

}
